package com.zhiche.lisa.mdm.service.impl;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.zhiche.lisa.mdm.dao.mapper.RangeCalcLogMapper;
import com.zhiche.lisa.mdm.dao.model.RangeCalcLog;
import com.zhiche.lisa.mdm.service.IRangeCalcLogService;
import com.zhiche.lisa.mdm.service.upload.UploadService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 上传文件批量计算运距日志 服务实现类
 * </p>
 *
 * @author qichao
 * @since 2018-08-02
 */
@Service
public class RangeCalcLogServiceImpl extends ServiceImpl<RangeCalcLogMapper, RangeCalcLog> implements IRangeCalcLogService {

    @Autowired
    private UploadService uploadService;

    @Override
    public Page<RangeCalcLog> rangCalcLogLine(Page<RangeCalcLog> page) {
        if (page == null) return null;
        page.setOrderByField("gmt_upload");
        page.setAsc(false);
        EntityWrapper<RangeCalcLog> ew = whereRangeCalcLog(page);
        List<RangeCalcLog> list = baseMapper.rangCalcLogLine(page, ew);
        list.forEach(rangeCalcLog -> {
            rangeCalcLog.setCalcResult(StringUtils.isNotEmpty(rangeCalcLog.getCalcResult())?rangeCalcLog.getCalcResult().replace("success","成功"):"");
            rangeCalcLog.setUploadFileName(StringUtils.isNotEmpty(rangeCalcLog.getUploadFileName())? rangeCalcLog.getUploadFileName().replace("template/","").trim():"");
            rangeCalcLog.setResultFileName(StringUtils.isNotEmpty(rangeCalcLog.getResultFileName())? rangeCalcLog.getResultFileName().replace("template/","").trim():"");
        });
        page.setRecords(list);
        return page;
    }

    private EntityWrapper<RangeCalcLog> whereRangeCalcLog(Page<RangeCalcLog> page) {
        EntityWrapper<RangeCalcLog> ew = new EntityWrapper<>();
        // 上传文件名称
        if (page.getCondition() != null
                && Objects.nonNull(page.getCondition().get("gmtUploadStart"))
                && StringUtils.isNotEmpty(page.getCondition().get("gmtUploadStart").toString())) {
            ew.gt("gmt_upload", page.getCondition().get("gmtUploadStart").toString().trim());
        }
        if (page.getCondition() != null
                && Objects.nonNull(page.getCondition().get("gmtUploadEnd"))
                && StringUtils.isNotEmpty(page.getCondition().get("gmtUploadEnd").toString())) {
            ew.le("gmt_upload", page.getCondition().get("gmtUploadEnd").toString().trim());
        }
        return ew;
    }

    @Override
    public String getUrlByFileName(String fileName) {
        if(StringUtils.isNotEmpty(fileName)){
            String key = "template/" + fileName;
            EntityWrapper<RangeCalcLog> ew = new EntityWrapper<>();
            ew.eq("result_file_name",key);
            RangeCalcLog rangeCalcLog = this.selectOne(ew);
            if(Objects.nonNull(rangeCalcLog)){
                return uploadService.getStrUrl(key);
            }
        }
        return  null;
    }
}
